<?php


namespace Home\Controller;


use Think\Controller;

class LoginController extends Controller
{
    public function index(){
        if ($_POST){
            //获取post提交的登录信息
            $data=I('post.');
            $username=$data['username'];
//            $password=$data['password'];
            // dump($data);
            //链接数据库并查询数据的信息
            $field=m('membersuser')->where("username='$username'")->find();


            if ($data['username']!=$field['username']  || md5($data['password'])!=$field['password']){
                $this->error("账号或密码不正确");exit;
        }

//            echo 1;exit;
            $res=$this->check_verify($_POST['code']);
            if(!$res){
                $this->error("验证码错误");exit;
            }

            session('username',$data['username']);
           // 获取登录用户的UID存入session
            $uid=m('membersuser')->where("username='$username'")->getField('uid');
            session('uid',$uid);

            $this->success('登录成功',u('home/cart/index'));exit;


        }





        $this->display();
    }

    public function code(){

        $Verify =     new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length   = 3;
        $Verify->useNoise = true;
        $Verify->entry();
    }

    public function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }


    public function atuo(){

    session('username',null);
    $this->success('退出成功',u('home/index/index'));exit;



    }

}